<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col ::sm="{span:24}" :md="{span:12}" :xl="{span:6}">
            <a-form-item label="招生年度">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsnj">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :md="{span:12}" :xl="{span:6}">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+"-"+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :md="{span:12}" :xl="{span:6}">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zszym" >
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{i.zyh+"-"+ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="学习形式">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.qrzhfqrz">
                  <a-select-option v-for="i in selectMenu.xxlx" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :sm="{span:16}" :xl="{span:8}">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="loadData" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a-button @click="queryParam = {};loadData()" style="margin:0 10px">重置</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'" />
            </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-alert :message="'总条数：' + data.length" type="info" show-icon />
    <a-table ref="table" size="default" rowKey="key" :columns="columns" :loading="loading" :dataSource="data" :alert="options.alert"
      :rowSelection="options.rowSelection" :pagination="false" @change="handleTableChange">

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- <p style="text-align: right;margin-right: 20px;">共 {{data.length}} 条</p> -->
  </a-card>
</template>

<script>
  // import moment from 'moment'
  import {
    STable
  } from '~'
  import {
    mapState
  } from 'vuex'

  const selectArr = ['TYPE0311', 'TYPE0312', 'TYPE0126', 'TYPE0075', 'TYPE0056', 'TYPE0083', 'TYPE0125', 'TYPE0072']
  export default {
    name: 'Professional',
    components: {
      STable
    },
    data() {
      return {
        advanced: true,
        loading: false,
        data: [],
        // 查询参数
        queryParam: {
          zsjhlxm: '02',
          zsnj: '',
          zsyxm: '',
          qrzhfqrz: ''
        },
        postParams: {},
        // 表头
        // 表头
        columns: [{
            title: '序号',
            scopedSlots: {
              customRender: 'serial'
            }
          },
          {
            title: '招生年度',
            dataIndex: 'zsnj'
          },
          {
            title: '院系代码',
            dataIndex: 'zsyxm'
          },
          {
            title: '院系',
            dataIndex: 'dwmc'
          },
          {
            title: '专业代码',
            dataIndex: 'zszym'
          },
          {
            title: '专业',
            dataIndex: 'zymc'
          },
          {
            title: '学习形式',
            dataIndex: 'qrzhfqrzmz'
          },
          {
            title: '总计',
            dataIndex: 'zs'
          },
          {
            title: '学术学位计划',
            dataIndex: 'fzyxwjhrs'
          },
          {
            title: '专业学位计划',
            dataIndex: 'zyxwjhrs'
          }
        ],

        // custom table alert & rowSelection
        options: {
          alert: {
            show: false
          }
        },
        visible: false,
        confirmLoading: false,
        editId: null,
        selectMenu: {
          yx: [], // 院系
          zy: [], // 所属专业
          xxlx: [] //学习形式
        },
        professionalAddVisiable: false,
        professionalEditVisiable: false,
        professionalEditData: {
          hzfs: '',
          zjh: '',
          fzyxwjh: '',
          zyxwjh: ''
        },
        pagination: {
          current: 1
        }
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created() {
      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu.yx = res[0].data
          this.queryParam.zsyxm = res[0].data[0].dwh
           this.getProssion(this.queryParam.zsyxm)
          // 获取学习形式
          this.$api.base.dictionaryData('TYPE0441').then(res => {
            this.selectMenu.xxlx = res.data
          })
          // 获取当前学年
          this.$api.base.getSchoolYear().then(res => {
            this.queryParam.zsnj = res.data.masterAdmissionYear
            this.loadData({
              pageSize: 10,
              pageNum: 1
            })
          })
        })
      },
      // 选择院系
      yxChange(code) {
        console.log(code)
        this.postParams.zsyxm = code
        // var yx = this.selectMenu.yx
        // var xxlx = this.selectMenu.xxlx
        this.$set(this.queryParam,'zszym','')
        this.getProssion(code)
      },
      getProssion(code) {
        this.$api.base.majorAll({
          lsdwh: code
        }).then(res => {
          if (res.data) {
            this.selectMenu.zy = res.data
          }
        })
      },
      // 选择招生年度
      zsndChange() {},
      // 加载数据
      loadData(parameter = {}) {
        if (!parameter.pageNum) {
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        let params = Object.assign(this.queryParam, parameter)
        this.loading = true;
        this.$api.recruitstat.prossionTotalList(params).then(res => {
          // console.log(res)
          const pagination = { ...this.pagination
          };
          pagination.total = 0
          if (res.data && res.data.total) {
            pagination.total = res.data.total;
          }
          this.data = []
          this.pagination = pagination;
          if (res.data && res.data.length) {
            res.data.map((item, index) => {
              this.selectMenu.xxlx.map((_item, _index) => {
                if (item.qrzhfqrz == _item.code) {
                  item.qrzhfqrzmz = _item.name
                }
              })
              return item;
            })
            this.data = res.data;
          }
          this.loading = false;
        })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData({
          pageSize: 10,
          pageNum: pagination.current
        });
      },
      // 导出
      exportExcel() {
        console.log('导出')
        this.$export('admissions/fpzyzsjh/exportfpzyzsjh', this.queryParam)
      },
      // 添加
      handleCreate() {
        this.professionalAddVisiable = true
      },
      // 关闭添加弹层
      handleCreateClose() {
        this.professionalAddVisiable = false
      },
      // 添加成功
      handleCreateSuccess() {
        this.professionalAddVisiable = false
        this.$message.success('添加成功')
        this.loadData({
          pageSize: 10,
          pageNum: 1
        })
      },
      // 修改
      handleEdit(key) {
        this.$api.recruitstat.disProsseionPlanId(key).then(res => {
          this.professionalEditVisiable = true
          if (res.data) {
            this.professionalEditData = res.data
          }
        })
      },
      // 关闭添加弹层
      handleEditClose() {
        this.professionalEditVisiable = false
      },
      // 添加成功
      handleEditSuccess() {
        this.professionalEditVisiable = false
        this.$message.success('修改成功')
        this.loadData({
          pageSize: 10,
          pageNum: this.pagination.current
        })
      }
    }
  }
</script>
